package com.apetrenko.avaya.callstore.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class LoginPanel extends Composite {

	private class LoginListener implements AsyncCallback<AuthResult>,
			ClickHandler {

		public void onFailure(Throwable aCaught) {
			showMessage("Authentication error!");
			setLocked(false);
		}

		@Override
		public void onSuccess(AuthResult aResult) {
			iPasswdBox.setText("");
			setLocked(false);
			if (aResult.isValid()) {
				showMessage("Success");
				CallStore.get().startSession(aResult.iId);
				hideMessage();
			} else {
				showMessage("Invalid username or password");
			}
		}

		@Override
		public void onClick(ClickEvent aEvent) {
			setLocked(true);
			showMessage("Authenticating...");

			String vLogin = iLoginBox.getText();
			String vPasswd = iPasswdBox.getText();

			LoginServiceAsync vService = GWT.create(LoginService.class);
			vService.authenticate(vLogin, vPasswd, this);
		}

	}

	private FlexTable iTable = new FlexTable();
	private TextBox iLoginBox = new TextBox();
	private PasswordTextBox iPasswdBox = new PasswordTextBox();
	private Button iLoginBtn = new Button();
	private Label iMessage;

	public LoginPanel() {
		for (int i = 0; i < 6; i++) {
			iTable.insertRow(0);
		}

		iLoginBtn.setText("Log in");
		iLoginBtn.addClickHandler(new LoginListener());

		FlexCellFormatter cf = (FlexCellFormatter) iTable.getCellFormatter();

		iTable.setWidget(0, 0, new HTML("<big>Log In</big>"));
		cf.setColSpan(0, 0, 2);
		cf.setHorizontalAlignment(0, 0, HasAlignment.ALIGN_CENTER);

		iTable.setWidget(1, 0, new Label("Username"));
		iTable.setWidget(2, 0, new Label("Password"));
		iLoginBox.setWidth("170px");
		iPasswdBox.setWidth("170px");
		iTable.setWidget(1, 1, iLoginBox);
		iTable.setWidget(2, 1, iPasswdBox);

		iTable.setWidget(4, 0, iLoginBtn);
		cf.setColSpan(4, 0, 2);
		cf.setHorizontalAlignment(4, 0, HasAlignment.ALIGN_CENTER);

		iMessage = new Label();
		iMessage.setVisible(false);
		iTable.setWidget(5, 0, iMessage);
		cf.setColSpan(5, 0, 2);
		cf.setHorizontalAlignment(5, 0, HasAlignment.ALIGN_CENTER);

		initWidget(iTable);
		setWidth("300px");
	}

	private void showMessage(String aMessage) {
		iMessage.setText(aMessage);
		if (!iMessage.isVisible()) {
			iMessage.setVisible(true);
		}
	}

	private void hideMessage() {
		if (iMessage.isVisible()) {
			iMessage.setVisible(false);
		}
		iMessage.setText("");
	}

	private void setLocked(boolean aLocked) {
		iLoginBox.setEnabled(!aLocked);
		iPasswdBox.setEnabled(!aLocked);
		iLoginBtn.setEnabled(!aLocked);
	}

}
